//
//  DataSource.m
//  小慕餐厅
//
//  Created by Mac on 2018/1/1.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "DataSource.h"
//#import "LocationServer.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@interface DataSource(){
//    NSString *currentCity;
//    int pd_num;
//    int fruitNum;
//    NSMutableArray *pd_array;
//    NSMutableArray *pd_food_array;
//    NSMutableArray *pd_fruit_array;
//    NSMutableArray *pd_order_array;
//    FMDatabase *fmdb;
    BOOL loginFlag;
}
@end

@implementation DataSource
// 1 全局变量
static id _instance = nil;
// 2 单例方法
+(instancetype)getInstance{
    return [[self alloc]init];
}
// 3 初始化方法 只执行一次
-(instancetype)init{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super init];
//        pd_num = 0;
//        fruitNum = 0;
//        pd_array = [[NSMutableArray alloc]init];
//        pd_food_array = [[NSMutableArray alloc]init];
//        pd_fruit_array = [[NSMutableArray alloc]init];
//        pd_order_array = [[NSMutableArray alloc]init];
  ////////////////////////////////////////////      self->loginFlag = false;   //登錄狀態
//        //数据库文件 表 增删改查
//        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject];
//        NSString *dbPath = [docPath stringByAppendingString:@"test.db"];
//        // 创建
//        fmdb = [FMDatabase databaseWithPath:dbPath];
//        // 打开
//        if ([fmdb open]) {
//            NSLog(@"打开成功");
//            NSString *sql = [NSString stringWithFormat:@"create table if not exists table1(id INTEGER PRIMARY KEY AUTOINCREMENT,text TEXT)"];
//            BOOL success = [fmdb executeUpdate:sql];
//            if (success) {
//                NSLog(@"表创建成功");
//            }else{
//                NSLog(@"表创建失败");
//            }
//        }else{
//            NSLog(@"打开失败");
//        }
    });
    
    return _instance;
}
//-(void)getLocationData{
//    [[LocationServer getInstance]startLocationServer];
//}
//
//-(void)setCurrentCity:(NSString*)str{
//    currentCity = str;
//}
//-(NSString*)getCurrentCity{
//    return currentCity;
//}
//-(int)getPDNum{
//    return pd_num;
//}
//-(void)setPdNum:(int)num{
//    pd_num = num;
//}
//-(void)loadPdItem:(ProductDataSource*)item{
//    [pd_array addObject:item];
//}
//-(NSMutableArray*)getPdArray{
//    return pd_array;
//}
//-(void)loadFruitItem:(ProductDataSource*)item{
//    [pd_fruit_array addObject:item];
//}
//-(NSMutableArray*)getFruitArray{
//    return pd_fruit_array;
//}
//-(void)loadFoodItem:(ProductDataSource*)item{
//    [pd_food_array addObject:item];
//}
//-(NSMutableArray*)getFoodArray{
//    return pd_food_array;
//}
//-(int)getFruitNum{
//    return fruitNum;
//}
//-(void)setFruitNum:(int)num{
//    fruitNum = num;
//}
//
//-(void)loadOrderItem:(ProductDataSource*)item{
//    [pd_order_array addObject:item];
//}
//-(NSMutableArray *)getOrderArray{
//    return pd_order_array;
//}
//
//-(FMDatabase*)getFMDB{
//    return fmdb;
//}
//- (void)add:(NSString *)str{
//    BOOL success = [fmdb executeUpdate:@"INSERT INTO table1(text) VALUES(?);",str];
//    if (success) {
//        NSLog(@"INSERT SUCCESS");
//    }else{
//        NSLog(@"INSERT ERROR");
//    }
//}
//- (void)search {
//    NSString *sql = @"SELECT * FROM table1;";
//    FMResultSet *set = [fmdb executeQuery:sql];
//    while ([set next]) {
//        NSString *text = [set stringForColumn:@"text"];
//        NSLog(@"%@",text);
//    }
//}
// login
-(void)setLoginFlag:(BOOL)flag{
    loginFlag = flag;
}
-(BOOL)getLoginFlag{
    return loginFlag;
}

-(NSString *)getCurrentDeviceModel{
    int mib[2];
    size_t len;
    char *machine;
    
    mib[0] = CTL_HW;
    mib[1] = HW_MACHINE;
    sysctl(mib, 2, NULL, &len, NULL, 0);
    machine = malloc(len);
    sysctl(mib, 2, machine, &len, NULL, 0);
    
    NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
    free(machine);
    // iPhone
    if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone2G";
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone3G";
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone3GS";
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone4";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone4";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone4";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone4S";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone5";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone5";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone5c";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone5c";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone5s";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone5s";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone6";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus";
    if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone6s";
    if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone6sPlus";
    if ([platform isEqualToString:@"iPhone8,3"]) return @"iPhoneSE";
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhoneSE";
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone7";
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone7Plus";
    return platform;
}
@end
